package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Properties;
import com.xxx.entity.dto.PropertiesDto;
import com.xxx.service.IPropertiesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 物业信息表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/properties")
@Tag(name = "物业信息表")
public class PropertiesController {

    @Autowired
    private IPropertiesService propertiesService;

    @PostMapping("/add")
    @Operation(summary = "添加用户")
    public Result add(@RequestBody Properties properties){
        Boolean flag = propertiesService.save(properties);
        return flag ? Result.success("添加用户成功") : Result.error("添加用户失败");
    }

    @GetMapping("/delete/{propertyId}")
    @Operation(summary = "删除用户")
    public Result delete(@PathVariable Integer propertyId){
        Boolean flag = propertiesService.removeById(propertyId);
        return flag ? Result.success("删除用户成功") : Result.error("不存在该用户Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改用户")
    public Result update(@RequestBody Properties properties){
        Boolean flag = propertiesService.updateById(properties);
        return flag ? Result.success("修改用户成功") : Result.error("修改用户失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询用户")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Properties> page = propertiesService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据物业Id、社区Id、住址、类型、状态条件查询用户")
    public Result selectByCondition(@RequestBody PropertiesDto propertiesDto){
        LambdaQueryWrapper<Properties> lqw = new LambdaQueryWrapper<>();
        lqw.like(propertiesDto.getAddress()!=null&&!propertiesDto.getAddress().equals(""),
                        Properties::getAddress,propertiesDto.getAddress())
                .eq(propertiesDto.getPropertyType()!=null&&!propertiesDto.getPropertyType().equals(""),
                        Properties::getPropertyType,propertiesDto.getPropertyType())
                .eq(propertiesDto.getCommunityId()!=null,Properties::getCommunityId,propertiesDto.getCommunityId())
                .eq(propertiesDto.getPropertyStatus()!=null,Properties::getPropertyStatus,propertiesDto.getPropertyStatus())
                .eq(propertiesDto.getPropertyId()!=null,Properties::getPropertyId,propertiesDto.getPropertyId());
        IPage<Properties> page = propertiesService.page(new Page<>(propertiesDto.getCurrent(), propertiesDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

